﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml;
using System.Linq;

namespace Common.DataTypes
{
    [XmlRoot("ItemSearchResponse", Namespace = "http://webservices.amazon.com/AWSECommerceService/2005-10-05")]
    public class AmazonBookCollection
    {
        //[XmlArray("Items"), XmlArrayItem("Item", typeof(AmazonBook))]
        //public List<AmazonBook> BookList { get; set; }

        [XmlElement("Items")]
        public SearchResultData ResultData { get; set; }

        public AmazonBookCollection() {
            this.ResultData = new SearchResultData();
        }

        public List<Book> GetBooks()
        {
            List<Book> ListResult = new List<Book>();
            AmazonBook[] ResultList = ResultData.BookList;

            if (ResultList != null)
            {
                foreach (AmazonBook abook in ResultList)
                {
                    ListResult.Add(ConvertAmazonBookToBook(abook));
                }
            }

            return (ListResult);
        }

        public int GetTotalResults(){
            return (ResultData.TotalResults);
        }

        private Book ConvertAmazonBookToBook(AmazonBook amazonBook) {
            Book bookToConvert = new Book();

            #region Load Book Data
            bookToConvert.Asin = amazonBook.Asin;
            bookToConvert.Author = amazonBook.Attributes.Author;
            bookToConvert.Binding = amazonBook.Attributes.Binding;
            bookToConvert.DetailPageUrl = amazonBook.DetailPageUrl;
            bookToConvert.Ean = amazonBook.Attributes.Ean;
            bookToConvert.Isbn = amazonBook.Attributes.Isbn;
            bookToConvert.Label = amazonBook.Attributes.Label;

            #region Load images
            if (amazonBook.SmallImage != null)
            {
                bookToConvert.SmallImage = amazonBook.SmallImage.Url;
            }
            if (amazonBook.MediumImage != null)
            {
                bookToConvert.MediumImage = amazonBook.MediumImage.Url;
            }
            if (amazonBook.LargeImage != null)
            {
                bookToConvert.LargeImage = amazonBook.LargeImage.Url;
            }
            #endregion

            // Load List Price
            ListPrice listPrice = amazonBook.Attributes.ListPrice;
            if (listPrice != null)
            {
                ListPriceData ListPriceData = new ListPriceData(int.Parse(listPrice.Amount), listPrice.FormattedPrice, listPrice.CurrencyCode);
                bookToConvert.ListPrice = ListPriceData;
            }

            #region Load Offer Summary
            if (amazonBook.OfferSummary != null) {
                ListPriceData listPriceData;
                ListPrice listPriceAux;
                ItemOfferSummaryData itemOfferSummary = new ItemOfferSummaryData();

                // Load offers of all conditions
                if (amazonBook.OfferSummary.LowestCollectiblePrice != null) { 
                    listPriceAux = amazonBook.OfferSummary.LowestCollectiblePrice;
                    listPriceData = new ListPriceData(int.Parse(listPriceAux.Amount), listPriceAux.FormattedPrice, listPriceAux.CurrencyCode);

                    itemOfferSummary.LowestCollectiblePrice = listPriceData;
                }
                if (amazonBook.OfferSummary.LowestNewPrice != null)
                {
                    listPriceAux = amazonBook.OfferSummary.LowestNewPrice;
                    listPriceData = new ListPriceData(int.Parse(listPriceAux.Amount), listPriceAux.FormattedPrice, listPriceAux.CurrencyCode);

                    itemOfferSummary.LowestNewPrice = listPriceData;
                }
                if (amazonBook.OfferSummary.LowestRefurbishedPrice != null)
                {
                    listPriceAux = amazonBook.OfferSummary.LowestRefurbishedPrice;
                    listPriceData = new ListPriceData(int.Parse(listPriceAux.Amount), listPriceAux.FormattedPrice, listPriceAux.CurrencyCode);

                    itemOfferSummary.LowestRefurbishedPrice = listPriceData;
                }
                if (amazonBook.OfferSummary.LowestUsedPrice != null)
                {
                    listPriceAux = amazonBook.OfferSummary.LowestUsedPrice;
                    listPriceData = new ListPriceData(int.Parse(listPriceAux.Amount), listPriceAux.FormattedPrice, listPriceAux.CurrencyCode);

                    itemOfferSummary.LowestUsedPrice = listPriceData;
                }

                // Load number of item per condition
                itemOfferSummary.TotalCollectible = amazonBook.OfferSummary.TotalCollectible;
                itemOfferSummary.TotalNew = amazonBook.OfferSummary.TotalNew;
                itemOfferSummary.TotalRefurbished = amazonBook.OfferSummary.TotalRefurbished;
                itemOfferSummary.TotalUsed = amazonBook.OfferSummary.TotalUsed;

                bookToConvert.OfferSummary = itemOfferSummary;
            }
            #endregion

            bookToConvert.Manufacturer = amazonBook.Attributes.Manufacturer;
            bookToConvert.PublicationDate = amazonBook.Attributes.PublicationDate;
            bookToConvert.Publisher = amazonBook.Attributes.Publisher;
            bookToConvert.ReleaseDate = amazonBook.Attributes.ReleaseDate;
            bookToConvert.SalesRank = amazonBook.SalesRank;
            
            bookToConvert.Studio = amazonBook.Attributes.Studio;
            bookToConvert.Title = amazonBook.Attributes.Title;
            #endregion

            return (bookToConvert);
        }
    }

    [XmlRoot("Items", Namespace = "http://webservices.amazon.com/AWSECommerceService/2005-10-05")]
    public class SearchResultData
    {
        [XmlElement("Item")]
        public AmazonBook[] BookList { get; set; }

        [XmlElement("TotalResults", Type=typeof(int))]
        public int TotalResults { get; set; }

        [XmlElement("TotalPages", Type=typeof(int))]
        public int TotalPages { get; set; }

        public SearchResultData() { 
            this.BookList = null;
            this.TotalPages = 0;
            this.TotalResults = 0;
        }
    }

}
